package com.ws.lv.controller;

import com.ws.lv.bean.Route;
import com.ws.lv.bean.Supplier;
import com.ws.lv.bean.Type;
import com.ws.lv.service.RouteService;
import com.ws.lv.service.SupplierService;
import com.ws.lv.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;


@Controller
@RequestMapping("/adminpage")
public class AdminPageController {

    @Autowired
    RouteService routeService;
    @Autowired
    TypeService typeService;
    @Autowired
    SupplierService supplierService;

    // 后台首页
    @RequestMapping("/toAdminPage")
    public String toAdminPage() {
        System.out.println("跳转到后台首页");
        return "adminpage/index";
    }

    // 后台欢迎页
    @RequestMapping("/toWelcome")
    public String toWelcome() {
        System.out.println("跳转到后台欢迎页");
        return "adminpage/welcome";
    }

    // 后台统计页
    @RequestMapping("/toCount")
    public String toStatistics() {
        System.out.println("跳转到后台统计页");
        return "adminpage/count";
    }

    //获取统计图数据 (供应商)
    @GetMapping("/supplierRoutes")
    @ResponseBody
    public Map<String, Object> supplierRoutes() {
        Map<String, Object> data = new HashMap<>();
        List<String> supplierNames = new ArrayList<>();
        List<Integer> supplierRoutes = new ArrayList<>();
        //获取类别 and 线路
        List<Supplier> suppliers = supplierService.selectSupplierAll();
        for (Supplier supplier : suppliers) {
            List<Route> routes = routeService.selectRoutesBySupplierId(supplier.getSupplier_id());
            supplierNames.add(supplier.getSupplier_name());
            supplierRoutes.add(routes.size());
        }

        data.put("supplierNames", supplierNames);
        data.put("supplierRoutes", supplierRoutes);
        System.out.println("Returning data: " + data); // 添加调试信息
        return data;
    }


    //获取统计图数据 (类别)
    @GetMapping("/typeRoutes")
    @ResponseBody
    public Map<String, Object> typeRoutes() {
        Map<String, Object> data = new HashMap<>();
        List<String> typeNames = new ArrayList<>();
        List<Integer> typeRoutes = new ArrayList<>();
        //获取类别 and 线路
        List<Type> types = typeService.selectTypeAll();
        for (Type type : types) {
            List<Route> routes = routeService.selectRoutesByType(type.getType_id());
            typeNames.add(type.getType_name());
            typeRoutes.add(routes.size());
        }

        data.put("typeNames", typeNames);
        data.put("typeRoutes", typeRoutes);
        System.out.println("Returning data: " + data); // 添加调试信息
        return data;
    }


    //获取统计图数据 (类别收藏量)
    @GetMapping("/saveTypeRoutes")
    @ResponseBody
    public Map<String, Object> saveTypeRoutes() {
        Map<String, Object> data = new HashMap<>();
        List<String> typeNames = new ArrayList<>();
        List<Integer> saveTypeRoutes = new ArrayList<>();
        //获取类别 and 线路
        List<Type> types = typeService.selectTypeAll();
        int saveNumber = 0;
        for (Type type : types) {
            List<Route> routes = routeService.selectRoutesByType(type.getType_id());
            typeNames.add(type.getType_name());
            for (Route route : routes) {
                saveNumber+=route.getRoute_saveNumber();
            }
            saveTypeRoutes.add(saveNumber);

        }

        data.put("typeNames", typeNames);
        data.put("saveTypeRoutes", saveTypeRoutes);
        System.out.println("Returning data: " + data); // 添加调试信息
        return data;
    }





}
